package com.prs.crm.action.ca.work;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.warning.AutoCodeAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.domain.ca.work.PurchaseApply;
import com.prs.crm.domain.ca.work.PurchaseAppplyDetail;
import com.prs.crm.domain.flow.FlowNodePerson;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.flow.WorkFlow;
import com.prs.crm.domain.flow.WorkFlowAndPerson;
import com.prs.crm.domain.flow.WorkFlowInstance;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.Form;
import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.ca.work.ArticleTypesService;
import com.prs.crm.service.ca.work.DemandInventoryService;
import com.prs.crm.service.ca.work.PurchaseApplyService;
import com.prs.crm.service.ca.work.PurchaseAppplyDetailService;
import com.prs.crm.service.flow.FlowNodePersonService;
import com.prs.crm.service.flow.FlowVerifyDetailService;
import com.prs.crm.service.flow.WorkFlowAndPersonService;
import com.prs.crm.service.flow.WorkFlowInstanceService;
import com.prs.crm.service.flow.WorkFlowNodeService;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.flow.WorkFlowService;
import com.prs.crm.service.sys.FormService;
import com.prs.crm.service.warning.AutoCodeService;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:34:42 采购申请
 */
public class PurchaseApplyAction extends BaseAction {

	private static final long serialVersionUID = 729362678331616109L;
	private WorkFlowInstanceService workflowInstanceService;
	private WorkFlowAndPerson workFlowAndPerson;
	private WorkFlowAndPersonService workFlowAndPersonService;
	private PurchaseApplyService purchaseApplyService;
	private PaginationSupport pagination;
	private PurchaseApply purchaseApply;
	private Application application;
	private ApplicationService ApplicationService;
	private Integer[] ids;
	private String pa_billNumber;
	private String pa_applyPerson;
	private String pa_applyDate;
	private String pa_totalMoney;
	private String[] di_articleName;
	private String[] di_articleNumber;
	private String[] di_articleUnivalence;
	private String[] prototalPrices;
//	private String[] di_articletype;
	private String[] di_articleStandard;
	private String[] di_articleUnit;
	private ArticleTypes[] di_articletype;
	private ShenQingTypeService shenQingTypeService;

	private List<DemandInventory> demandInventory;
	private DemandInventoryService demandInventoryService;

	private List<ArticleTypes> articleTypes;
	private ArticleTypesService articleTypesService;

	private File upload;
	private String uploadFileName;

	private Form form;
	private FormService formService;
	private WorkFlowProcess workFlowProcess;
	private WorkFlowInstance workFlowInstance;
	private WorkFlowProcessService workflowProcessService;
	private WorkFlowProcessNode node;
	private Collection<WorkFlowProcessNode> endNodes;
	private File attach;
	private String attachFileName;
	private FlowVerifyDetail detail;
	private List<WorkFlow> workFlow;
	private WorkFlow workFlows;
	private WorkFlowService workflowService;
	private List<FlowNodePerson> nodePerson;
	private FlowNodePersonService flowNodePersonService;
	private WorkFlowNodeService workFlowNodeService;
	private FlowVerifyDetailService flowVerifyDetailService;
	private File uploads;
	private String uploadsFileName;
	private FlowVerifyDetail flowVerifyDetail;
	private AutoCodeService autoCodeService;
	private String codeNum;
	private PurchaseAppplyDetailService purchaseAppplyDetailService;
	private List<PurchaseAppplyDetail> details;
	private List<WorkFlowAndPerson> workFlowAndPersons;
	private List<FlowVerifyDetail> flowVerifyDetails;
public List<WorkFlowAndPerson> getWorkFlowAndPersons() {
		return workFlowAndPersons;
	}
	public void setWorkFlowAndPersons(List<WorkFlowAndPerson> workFlowAndPersons) {
		this.workFlowAndPersons = workFlowAndPersons;
	}
	public List<FlowVerifyDetail> getFlowVerifyDetails() {
		return flowVerifyDetails;
	}
	public void setFlowVerifyDetails(List<FlowVerifyDetail> flowVerifyDetails) {
		this.flowVerifyDetails = flowVerifyDetails;
	}
public List<PurchaseAppplyDetail> getDetails() {
		return details;
	}
	public void setDetails(List<PurchaseAppplyDetail> details) {
		this.details = details;
	}
public ArticleTypes[] getDi_articletype() {
		return di_articletype;
	}
	public void setDi_articletype(ArticleTypes[] diArticletype) {
		di_articletype = diArticletype;
	}
	//	public String[] getDi_articletype() {
//		return di_articletype;
//	}
//	public void setDi_articletype(String[] diArticletype) {
//		di_articletype = diArticletype;
//	}
	public String[] getDi_articleStandard() {
		return di_articleStandard;
	}
	public void setDi_articleStandard(String[] diArticleStandard) {
		di_articleStandard = diArticleStandard;
	}
	public String[] getDi_articleUnit() {
		return di_articleUnit;
	}
	public void setDi_articleUnit(String[] diArticleUnit) {
		di_articleUnit = diArticleUnit;
	}
	public PurchaseAppplyDetailService getPurchaseAppplyDetailService() {
		return purchaseAppplyDetailService;
	}
	public void setPurchaseAppplyDetailService(
			PurchaseAppplyDetailService purchaseAppplyDetailService) {
		this.purchaseAppplyDetailService = purchaseAppplyDetailService;
	}
	public String[] getDi_articleName() {
		return di_articleName;
	}
	public void setDi_articleName(String[] diArticleName) {
		di_articleName = diArticleName;
	}
	public String[] getDi_articleNumber() {
		return di_articleNumber;
	}
	public void setDi_articleNumber(String[] diArticleNumber) {
		di_articleNumber = diArticleNumber;
	}
	public String[] getDi_articleUnivalence() {
		return di_articleUnivalence;
	}
	public void setDi_articleUnivalence(String[] diArticleUnivalence) {
		di_articleUnivalence = diArticleUnivalence;
	}
	public String[] getPrototalPrices() {
		return prototalPrices;
	}
	public void setPrototalPrices(String[] prototalPrices) {
		this.prototalPrices = prototalPrices;
	}
	public String getCodeNum() {
		return codeNum;
	}
	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}
	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}
	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}
	public FlowVerifyDetail getDetail() {
		return detail;
	}
	public FlowVerifyDetail getFlowVerifyDetail() {
		return flowVerifyDetail;
	}

	public void setFlowVerifyDetail(FlowVerifyDetail flowVerifyDetail) {
		this.flowVerifyDetail = flowVerifyDetail;
	}

	public File getUploads() {
		return uploads;
	}

	public void setUploads(File uploads) {
		this.uploads = uploads;
	}

	public String getUploadsFileName() {
		return uploadsFileName;
	}

	public void setUploadsFileName(String uploadsFileName) {
		this.uploadsFileName = uploadsFileName;
	}

	public FlowVerifyDetailService getFlowVerifyDetailService() {
		return flowVerifyDetailService;
	}

	public void setFlowVerifyDetailService(
			FlowVerifyDetailService flowVerifyDetailService) {
		this.flowVerifyDetailService = flowVerifyDetailService;
	}

	public WorkFlowNodeService getWorkFlowNodeService() {
		return workFlowNodeService;
	}

	public void setWorkFlowNodeService(WorkFlowNodeService workFlowNodeService) {
		this.workFlowNodeService = workFlowNodeService;
	}

	public WorkFlow getWorkFlows() {
		return workFlows;
	}

	public void setWorkFlows(WorkFlow workFlows) {
		this.workFlows = workFlows;
	}

	public List<FlowNodePerson> getNodePerson() {
		return nodePerson;
	}

	public void setNodePerson(List<FlowNodePerson> nodePerson) {
		this.nodePerson = nodePerson;
	}

	public FlowNodePersonService getFlowNodePersonService() {
		return flowNodePersonService;
	}

	public void setFlowNodePersonService(
			FlowNodePersonService flowNodePersonService) {
		this.flowNodePersonService = flowNodePersonService;
	}

	public WorkFlowService getWorkflowService() {
		return workflowService;
	}

	public void setWorkflowService(WorkFlowService workflowService) {
		this.workflowService = workflowService;
	}

	public List<WorkFlow> getWorkFlow() {
		return workFlow;
	}

	public void setWorkFlow(List<WorkFlow> workFlow) {
		this.workFlow = workFlow;
	}

	public File getAttach() {
		return attach;
	}

	public void setAttach(File attach) {
		this.attach = attach;
	}

	public String getAttachFileName() {
		return attachFileName;
	}

	public void setAttachFileName(String attachFileName) {
		this.attachFileName = attachFileName;
	}

	public void setDetail(FlowVerifyDetail detail) {
		this.detail = detail;
	}

	public Collection<WorkFlowProcessNode> getEndNodes() {
		return endNodes;
	}

	public void setEndNodes(Collection<WorkFlowProcessNode> endNodes) {
		this.endNodes = endNodes;
	}

	public WorkFlowProcessNode getNode() {
		return node;
	}

	public void setNode(WorkFlowProcessNode node) {
		this.node = node;
	}

	public WorkFlowProcessService getWorkflowProcessService() {
		return workflowProcessService;
	}

	public void setWorkflowProcessService(
			WorkFlowProcessService workflowProcessService) {
		this.workflowProcessService = workflowProcessService;
	}

	public void setWorkflowInstanceService(
			WorkFlowInstanceService workflowInstanceService) {
		this.workflowInstanceService = workflowInstanceService;
	}

	public WorkFlowInstanceService getWorkflowInstanceService() {
		return workflowInstanceService;
	}

	public WorkFlowInstance getWorkFlowInstance() {
		return workFlowInstance;
	}

	public void setWorkFlowInstance(WorkFlowInstance workFlowInstance) {
		this.workFlowInstance = workFlowInstance;
	}

	public WorkFlowProcess getWorkFlowProcess() {
		return workFlowProcess;
	}

	public void setWorkFlowProcess(WorkFlowProcess workFlowProcess) {
		this.workFlowProcess = workFlowProcess;
	}

	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	/**
	 * 明细数组
	 */
	// private String[] purcids;
	// private Integer[] productlist;
	// private List<ArticleTypes> di_articletype;
	// private String[] di_articleName;
	// private String[] di_articleStandard;
	// private String[] di_articleNumber;
	// private String[] di_articleUnit;
	// private String[] di_articleUnivalence;
	// private String[] prototalPrices;

	@Action("listPurchaseApply")
	public String listPurchaseApply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseApply.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPurchaseApplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectPurchaseApply")
	public String selectPurchaseApply() {
		this.listPurchaseApply();
		return SUCCESS;
	}

	@Action("editPurchaseApply")
	public String edit() {
		this.demandInventory = this.getDemandInventoryService()
				.getAllDemandInventory();
		this.articleTypes = this.getArticleTypesService().getAllArticleTypes();
		// this.form=this.getFormService().findByEname("PurchaseApply");
		this.workFlowInstance = this.getWorkflowInstanceService()
				.findByFormName("PurchaseApply");
		this.workFlow = this.getWorkflowService().getWorkFlow("办公用品流程");
		codeNum=getCode("CG");
		details=this.getPurchaseAppplyDetailService().getPurchaseAppplyDetail(purchaseApply);
		return SUCCESS;
	}

	@Action(value = "savePurchaseApply", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseApply"),
			@Result(name = "error", type = "chain", location = "editPurchaseApply") })
	public String savePurchaseApply() {
		// double totalPrice = 0.0;
		// for(int i=0;i<purcids.length;i++){
		// DemandInventory demoOb =
		// this.getDemandInventoryService().get(Integer.parseInt(purcids[i]));
		// demoOb.setDi_articletype(di_articletype.get(i));
		// demoOb.setDi_articleName(di_articleName[i]);
		// demoOb.setDi_articleNumber(di_articleNumber[i]);
		// demoOb.setDi_articleUnit(di_articleUnit[i]);
		// demoOb.setDi_articleUnivalence(di_articleUnivalence[i]);
		// demoOb.setDi_articleStandard(di_articleStandard[i]);
		// demoOb.setDi_money(prototalPrices[i]);
		// totalPrice +=Double.parseDouble(prototalPrices[i]);
		// purchaseApply.addDemandInventory(demoOb);
		// }

		purchaseApply.setPa_examineState(1);
		purchaseApply.setPa_examineProcedure(1);
		// purchaseApply.setPa_totalMoney(totalPrice);
		// workFlowProcess.setInstance(workFlowInstance);
		// this.getWorkflowProcessService().save(workFlowProcess);
		// this.purchaseApply.setProcess(workFlowProcess);
		int lengths=di_articleName.length;
		double totalPrice = 0.0;
		for (int i = 0; i < lengths; i++) {
			PurchaseAppplyDetail pas=new PurchaseAppplyDetail();
			pas.setArticleName(di_articleName[i]);
			pas.setArticleNumber(Integer.parseInt(di_articleNumber[i]));
			pas.setArticleStandard(di_articleStandard[i]);
			pas.setArticleUnit(di_articleUnit[i]);
			pas.setPrice(di_articleUnivalence[i]);
			pas.setMoney(prototalPrices[i]);
			DemandInventory dis=this.getDemandInventoryService().getDemandInventoryByNames(di_articleName[i]);
			if (dis!=null) {
				pas.setArticletype(dis.getDi_articletype());
			}
			totalPrice=totalPrice+Double.parseDouble(prototalPrices[i]);
			purchaseApply.addPurchaseAppplyDetail(pas);
		}
		purchaseApply.setPa_totalMoney(totalPrice);
		this.getPurchaseApplyService().saveApply(purchaseApply, upload,
				uploadFileName);
		
		
//		application = new Application();
//		application.setType(purchaseApply.getType());
//		application.setAppName("[" + purchaseApply.getType() + "]"
//				+ purchaseApply.getAppName());
//		application.setSqType(purchaseApply.getPa_examineState());
//		application.setSpr(purchaseApply.getPa_examineProcedure());
//		application.setPerson(purchaseApply.getRecorder());
//		application.setPurchaseApply(purchaseApply);
//		application.setResign("采购申请");
		// application.setShenQingType(this.getShenQingTypeService().getShenQingType("采购申请"));
//		this.getApplicationService().save(application);
//		this.getPurchaseApplyService().save(purchaseApply);

		List<FlowNodePerson> fp = this.getFlowNodePersonService()
				.getFlowNodePerson(purchaseApply.getWorkFlow());
		for (FlowNodePerson ob : fp) {
			WorkFlowAndPerson workFlowAndPerson = new WorkFlowAndPerson();
			workFlowAndPerson.setWorkFlow(ob.getWorkFlow());
//			workFlowAndPerson.setNodePersonId(String.valueOf(ob
//					.getNodePersonName().getId()));
			workFlowAndPerson.setNodePersonId(ob.getNodeperson_id());
			workFlowAndPerson.setFlowstate("1");// 审签状态
			workFlowAndPerson.setFlowsequen(ob.getWorkFlowNode().getSequence()
					+ "");
			workFlowAndPerson.setType(ob.getWorkFlowNode().getType()+"");
			String[] persons=ob.getNodeperson_id().split(",");
			System.out.println(persons.length);
			workFlowAndPerson.setPersonNum(persons.length);
			workFlowAndPerson.setPurchaseApply(purchaseApply);
			this.getWorkFlowAndPersonService().save(workFlowAndPerson);

		}
		return SUCCESS;
	}

	@Action(value = "saveStorage", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseApply"),
			@Result(name = "error", type = "chain", location = "editPurchaseApply") })
	public String saveStorage() {
		if (upload != null) {
			this.purchaseApply.setAttach(FileUtil.getInstance().createAttach(
					upload, uploadFileName));
		}
		purchaseApply.setPa_examineState(0);
		this.getPurchaseApplyService().save(purchaseApply);
		return SUCCESS;
	}

	@Action(value = "removePurchaseApply", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseApply"),
			@Result(name = "error", type = "chain", location = "listPurchaseApply") })
	public String removePurchaseApply() {
		if (ids == null) {
			this.addActionError("请选中一行操作！");
			return ERROR;
		}
		this.getPurchaseApplyService().batchRemove(ids);
		this.setPrompt("删除成功！");
		return SUCCESS;
	}

	// public Integer[] getProductlist() {
	// return productlist;
	// }
	//
	// public void setProductlist(Integer[] productlist) {
	// this.productlist = productlist;
	// }
	//
	// public List<ArticleTypes> getDi_articletype() {
	// return di_articletype;
	// }
	//
	// public void setDi_articletype(List<ArticleTypes> diArticletype) {
	// di_articletype = diArticletype;
	// }
	//
	// public String[] getDi_articleName() {
	// return di_articleName;
	// }
	//
	// public void setDi_articleName(String[] diArticleName) {
	// di_articleName = diArticleName;
	// }
	//
	// public String[] getDi_articleStandard() {
	// return di_articleStandard;
	// }
	//
	// public void setDi_articleStandard(String[] diArticleStandard) {
	// di_articleStandard = diArticleStandard;
	// }
	//
	// public String[] getDi_articleNumber() {
	// return di_articleNumber;
	// }
	//
	// public void setDi_articleNumber(String[] diArticleNumber) {
	// di_articleNumber = diArticleNumber;
	// }
	//
	// public String[] getDi_articleUnit() {
	// return di_articleUnit;
	// }
	//
	// public void setDi_articleUnit(String[] diArticleUnit) {
	// di_articleUnit = diArticleUnit;
	// }
	//
	// public String[] getDi_articleUnivalence() {
	// return di_articleUnivalence;
	// }
	//
	// public void setDi_articleUnivalence(String[] diArticleUnivalence) {
	// di_articleUnivalence = diArticleUnivalence;
	// }
	//
	// public String[] getPrototalPrices() {
	// return prototalPrices;
	// }
	//
	// public void setPrototalPrices(String[] prototalPrices) {
	// this.prototalPrices = prototalPrices;
	// }

	public File getUpload() {
		return upload;
	}

	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public ApplicationService getApplicationService() {
		return ApplicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<ArticleTypes> getArticleTypes() {
		return articleTypes;
	}

	public void setArticleTypes(List<ArticleTypes> articleTypes) {
		this.articleTypes = articleTypes;
	}

	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}

	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}

	public List<DemandInventory> getDemandInventory() {
		return demandInventory;
	}

	public void setDemandInventory(List<DemandInventory> demandInventory) {
		this.demandInventory = demandInventory;
	}

	public DemandInventoryService getDemandInventoryService() {
		return demandInventoryService;
	}

	public void setDemandInventoryService(
			DemandInventoryService demandInventoryService) {
		this.demandInventoryService = demandInventoryService;
	}

	public String getPa_billNumber() {
		return pa_billNumber;
	}

	public void setPa_billNumber(String paBillNumber) {
		pa_billNumber = paBillNumber;
	}

	public String getPa_applyPerson() {
		return pa_applyPerson;
	}

	public void setPa_applyPerson(String paApplyPerson) {
		pa_applyPerson = paApplyPerson;
	}

	public String getPa_applyDate() {
		return pa_applyDate;
	}

	public void setPa_applyDate(String paApplyDate) {
		pa_applyDate = paApplyDate;
	}

	public String getPa_totalMoney() {
		return pa_totalMoney;
	}

	public void setPa_totalMoney(String paTotalMoney) {
		pa_totalMoney = paTotalMoney;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public PurchaseApply getPurchaseApply() {
		return purchaseApply;
	}

	public void setPurchaseApply(PurchaseApply purchaseApply) {
		this.purchaseApply = purchaseApply;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PurchaseApplyService getPurchaseApplyService() {
		return purchaseApplyService;
	}

	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}

	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}

	public void setPurchaseApplyService(
			PurchaseApplyService purchaseApplyService) {
		this.purchaseApplyService = purchaseApplyService;
	}

	// public String[] getPurcids() {
	// return purcids;
	// }
	//
	// public void setPurcids(String[] purcids) {
	// this.purcids = purcids;
	// }

	@Action("listAppPurchaseApply")
	public String listAppPurchaseApply() {
	  Person currentperson = AppContext.getAccountContext().currentUser();
		List<PurchaseApply> pa = this.getPurchaseApplyService().getAllPurchaseApply();
		for(PurchaseApply  ob:pa)
		{
	    List<WorkFlowAndPerson> wf = this.getWorkFlowAndPersonService().getWorkFlowAndPerson(ob.getWorkFlow(),ob);
		//顺序调整
	    List<WorkFlowAndPerson> wff=  sequens(wf);
	    int slent=wff.size();
	   for(WorkFlowAndPerson obb:wff)
	   {
			   if("1".equals(obb.getFlowstate()))
			   {//为1就是未审签
				  if ("1".equals(obb.getType())) {	 
	//				String personss=  obb.getNodePersonId(); 
					  System.out.println(obb.getId());
					System.out.println(obb.getNodePersonId());
					String[] personss=obb.getNodePersonId().split(",");
					for(String persons:personss){
						System.out.println(persons);
					if((currentperson.getId()+"").equals(persons)){
						 if (ob.getPersonAppProcess()!=null) {
								String[] personapp=ob.getPersonAppProcess().split(",");
								 for (String pers : personapp) {
									if (currentperson.getResume().getName().equals(pers)) {
										ob.setPersonAppState(2);
										this.getPurchaseApplyService().submit(ob);
										break;
									}else{
										 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
										    ob.setPa_examineState(1);
										    ob.setPersonAppState(1);
										    this.getPurchaseApplyService().submit(ob);  
									}
								}
							}else{
								 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
								    ob.setPa_examineState(1);
								    ob.setPersonAppState(1);
								    this.getPurchaseApplyService().submit(ob);
							}
						 
						}else{
							
						}	
					}
					 break;
				 } 
				  if ("2".equals(obb.getType())) {	 
	//				String personss=  obb.getNodePersonId(); 
					System.out.println(obb.getNodePersonId());
					String[] personss=obb.getNodePersonId().split(",");
					for(String persons:personss){
						System.out.println(persons);
					if((currentperson.getId()+"").equals(persons))
						{//如果相同则 需要该人审签
							//设置是否显示
						if (ob.getPersonAppProcess()!=null) {
							String[] personapp=ob.getPersonAppProcess().split(",");
							 for (String pers : personapp) {
								if (currentperson.getResume().getName().equals(pers)) {
									ob.setPersonAppState(2);
									this.getPurchaseApplyService().submit(ob);
									break;
								}else{
									 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
									    ob.setPa_examineState(1);
									    ob.setPersonAppState(1);
									    this.getPurchaseApplyService().submit(ob);
								}
							}
						}else{
							 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
							    ob.setPa_examineState(1);
							    ob.setPersonAppState(1);
							    this.getPurchaseApplyService().submit(ob);
						}
						}else{
							
						}	
					}
					break;
				 }
				  if ("3".equals(obb.getType())) {	 
	//				String personss=  obb.getNodePersonId(); 
					System.out.println(obb.getNodePersonId());
					String[] personss=obb.getNodePersonId().split(",");
					for(String persons:personss){
						System.out.println(persons);
					if((currentperson.getId()+"").equals(persons))
						{//如果相同则 需要该人审签
							//设置是否显示
						if (ob.getPersonAppProcess()!=null) {
							String[] personapp=ob.getPersonAppProcess().split(",");
							 for (String pers : personapp) {
								if (currentperson.getResume().getName().equals(pers)) {
									ob.setPersonAppState(2);
									this.getPurchaseApplyService().submit(ob);
									break;
								}else{
									 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
									    ob.setPa_examineState(1);
									    ob.setPersonAppState(1);
									    this.getPurchaseApplyService().submit(ob);
								}
							}
						}else{
							 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
							    ob.setPa_examineState(1);
							    ob.setPersonAppState(1);
							    this.getPurchaseApplyService().submit(ob);
						}
						}else{
							
						}	
					}
					break;
				 }			 
				  if ("5".equals(obb.getType())) {	 
	//				String personss=  obb.getNodePersonId(); 
					System.out.println(obb.getNodePersonId());
					String[] personss=obb.getNodePersonId().split(",");
					for(String persons:personss){
						System.out.println(persons);
					if((currentperson.getId()+"").equals(persons))
						{//如果相同则 需要该人审签
							//设置是否显示
						if (ob.getPersonAppProcess()!=null) {
							String[] personapp=ob.getPersonAppProcess().split(",");
							 for (String pers : personapp) {
								if (currentperson.getResume().getName().equals(pers)) {
									ob.setPersonAppState(2);
									this.getPurchaseApplyService().submit(ob);
									break;
								}else{
									 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
									    ob.setPersonAppState(1);
									    this.getPurchaseApplyService().submit(ob);
								}
							}
						}else{
							 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
							    ob.setPersonAppState(1);
							    this.getPurchaseApplyService().submit(ob);
						}
						}else{
							
						}	
					}
					break;
				 }			  
	   		  } 	
		   }
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PurchaseApply.class);
		criteria.add(Restrictions.eq("pa_examineState", 1));// 1：未审核
		criteria.add(Restrictions.eq("showstate",AppContext.getAccountContext().currentUser().getId()+"：1"));//1：显示
		criteria.add(Restrictions.isNotNull("workFlow"));//审签流程不为空值	
		criteria.add(Restrictions.eq("personAppState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPurchaseApplyService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	/**
	 * 顺序调整
	 * 
	 */
	public  static List<WorkFlowAndPerson>  sequens(List<WorkFlowAndPerson> ss)
	{
		List<WorkFlowAndPerson>  returnlist=new ArrayList<WorkFlowAndPerson>();
		int slength=ss.size();
		for(int i=0;i<slength;i++)
		{
			  
			for(WorkFlowAndPerson  uo:ss)
			{
				if(uo.getFlowsequen().equals(i+""))
				{
					returnlist.add(uo);
				}
			}
		}
      
		
		return returnlist;
		
	}

	@Action("editApplPurchaseApply")
	public String editAppPurchaseApply() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveApplPurchaseApply", results = { @Result(name = "success", type = "redirect", location = "listAppPurchaseApply") })
	public String saveApplPurchaseApply() {
		List<WorkFlowAndPerson> wf=this.getWorkFlowAndPersonService().getWorkFlowAndPerson(purchaseApply.getWorkFlow(), purchaseApply);
		List<WorkFlowAndPerson> wfp=sequens(wf);
		for (WorkFlowAndPerson wff : wfp) {
			if("1".equals(wff.getFlowstate())){
				String[] personss=wff.getNodePersonId().split(",");
				for (String persons : personss) {
					System.out.println(persons);
					
					if((AppContext.getAccountContext().currentUser().getId()+"").equals(persons)){
					FlowVerifyDetail detail=new FlowVerifyDetail();
					if (uploads!=null) {
						this.flowVerifyDetail.setAttach(FileUtil.getInstance().createAttach(uploads, uploadsFileName));
						detail.setAttach(flowVerifyDetail.getAttach());
					}
					detail.setWorkFlowAndPerson(wff);
					detail.setExecutor(AppContext.getAccountContext().currentUser());
					detail.setResult(flowVerifyDetail.getResult());
					detail.setOpinion(flowVerifyDetail.getOpinion());
					this.getFlowVerifyDetailService().save(detail);
					if("5".equals(wff.getType())){//如果该节点是结束节点时，审核完成
						if (wff.getPersonNum()==1) {
							if (flowVerifyDetail.getResult()==4) {
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPa_examineState(4);
								purchaseApply.setPersonAppState(2);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}else if(flowVerifyDetail.getResult()==1){
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								purchaseApply.setShowstate("2");
								purchaseApply.setPa_examineState(2);
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPersonAppState(2);
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}
							break;
						}else{
							if (flowVerifyDetail.getResult()==4) {
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPa_examineState(4);
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPersonAppState(2);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}else if(flowVerifyDetail.getResult()==1){
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								purchaseApply.setShowstate("2");
								if (purchaseApply.getPersonAppProcess()==null || "".equals(purchaseApply.getPersonAppProcess().trim())) {
									purchaseApply.setPersonAppProcess(AppContext.getAccountContext().currentUser().getResume().getName());
								}else{
									purchaseApply.setPersonAppProcess(purchaseApply.getPersonAppProcess()+","+AppContext.getAccountContext().currentUser().getResume().getName());
								}
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}
							break;
						}
					}else{
						if (wff.getPersonNum()==1) {
							if (flowVerifyDetail.getResult()==4) {
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPa_examineState(4);
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPersonAppState(2);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}else if(flowVerifyDetail.getResult()==1){
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setShowstate("2");
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPersonAppState(2);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}
							break;
						}else{
							if (flowVerifyDetail.getResult()==4) {
								wff.setFlowstate("2");
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setPa_examineState(4);
								purchaseApply.setPersonAppProcess("");
								purchaseApply.setPersonAppState(2);
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}else if(flowVerifyDetail.getResult()==1){
								wff.setPersonNum(wff.getPersonNum()-1);
								this.getWorkFlowAndPersonService().submit(wff);
								purchaseApply.setShowstate("2");
								if (purchaseApply.getPersonAppProcess()==null || "".equals(purchaseApply.getPersonAppProcess().trim())) {
									purchaseApply.setPersonAppProcess(AppContext.getAccountContext().currentUser().getResume().getName());
								}else{
									purchaseApply.setPersonAppProcess(purchaseApply.getPersonAppProcess()+","+AppContext.getAccountContext().currentUser().getResume().getName());
								}
								purchaseApply.setPa_examinePerson(AppContext.getAccountContext().currentUser().getResume().getName());
								this.getPurchaseApplyService().submit(purchaseApply);
								this.setPrompt("提交成功");
								return SUCCESS;
							}
							break;
						}
					}
				}
//				break;
			}
		}
	}
		this.setPrompt("提交成功");
		return SUCCESS;
	}
	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
	    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
	    Date dat=new Date();
	    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
	    String date=adf.format(dat);
	    if(alist==null)
	    {//初始的时候调用
		    AutoCode  ac=new AutoCode();
		    ac.setCodeType(type);
		    ac.setCodeValue(type+date+"0000001");
		    ac.setAddValue("0000001");
		    this.getAutoCodeService().save(ac);	
		    return type+date+"0000001";
	    }else{
	    	String vac=alist.getAddValue();
	    	AutoCode alistA=this.getAutoCodeService().getTaskRemind(type);
	    	alistA.setCodeValue(type+date+new AutoCodeAction().addCodes(vac));
	    	alistA.setAddValue(new AutoCodeAction().addCodes(vac));
		    this.getAutoCodeService().submit(alistA);	
		    return type+date+new AutoCodeAction().addCodes(vac);
	    }
	}
	
	@Action(value = "viewAppPur", results = { @Result(name = "error", type = "chain", location = "listPurchaseApply") })
	public String viewAppPur(){
		if (ids!=null) {
			this.purchaseApply=this.getPurchaseApplyService().get(ids[0]);
			this.workFlowAndPersons=this.getWorkFlowAndPersonService().getWorkFlowAndPerson(purchaseApply.getWorkFlow(), purchaseApply);
			this.flowVerifyDetails=this.getFlowVerifyDetailService().getAllFlowVerifyDetails();
			return SUCCESS;
		}else{
			this.addActionError("请选择数据！");
			return ERROR;
		}
	}
	
	public WorkFlowAndPerson getWorkFlowAndPerson() {
		return workFlowAndPerson;
	}

	public void setWorkFlowAndPerson(WorkFlowAndPerson workFlowAndPerson) {
		this.workFlowAndPerson = workFlowAndPerson;
	}

	public WorkFlowAndPersonService getWorkFlowAndPersonService() {
		return workFlowAndPersonService;
	}

	public void setWorkFlowAndPersonService(
			WorkFlowAndPersonService workFlowAndPersonService) {
		this.workFlowAndPersonService = workFlowAndPersonService;
	}

}
